package com.inossem.warehousemanage_tv;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.adapter.MyTabAdapter;
import com.inossem.warehousemanage_tv.adapter.MyViewPageAdapter;
import com.inossem.warehousemanage_tv.constant.UrlConstant;
import com.inossem.warehousemanage_tv.custom.NoScrollHorizontalViewPager;
import com.inossem.warehousemanage_tv.fragment.BaseFragment;
import com.inossem.warehousemanage_tv.fragment.PurchaseFragment;
import com.inossem.warehousemanage_tv.fragment.WareFragment;
import com.inossem.warehousemanage_tv.fragment.WebFragment;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

public class MainActivity extends FragmentActivity {

    private VerticalTabLayout tabLayout;
    private NoScrollHorizontalViewPager viewPager;
    private TextView tvReturn;

    private List<BaseFragment> fragmentList;

    //缓存的参数↓↓↓ 避免fragment之间传值
    public String factory_id, location_id, area_id, position_id, position_index1;//请求接口需要的参数
    public Boolean isTemporary = false;//是否是临时存储区
    public Boolean isTemporaryShelves = false;//是否是临时存储区(货架页面)
    public String locationName, shelveName;//标题
    private PurchaseFragment purchaseFragment=null;
    private WareFragment wareFragment = null;
    public BaseFragment showFragment = null;

    //时间监听有关
    private IntentFilter intentFilter;
    private TimeChangeReceiver timeChangeReceiver;
    private final static Integer period = 3;//自动请求接口的时间间隔
    private Integer time = 0;//当前程序运行的是第几分钟

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActivity();
        setContentView(R.layout.activity_main);

        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());

        initView();
        initData();
        initClick();
        initReceiver();
    }

    private void initActivity() {
        //默认不弹出键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //强制横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    private void initView() {
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
        tvReturn = findViewById(R.id.tv_return);
    }

    private void initData() {
        wareFragment = new WareFragment();//仓库概览

        WebFragment webFragment_handle = new WebFragment();//待办事项
        Bundle bundle1 = new Bundle();
        bundle1.putString("url", UrlConstant.DEFAULT_HTTP + UrlConstant.DEFAULT_DOMAIN + ":" + UrlConstant.DEFAULT_PORT + UrlConstant.WEB_DEFAULT_PATH + UrlConstant.WEB_URL_AGENCY);
        webFragment_handle.setArguments(bundle1);

        WebFragment webFragment_frequency = new WebFragment();//库龄分析
        Bundle bundle2 = new Bundle();
        bundle2.putString("url", UrlConstant.DEFAULT_HTTP + UrlConstant.DEFAULT_DOMAIN + ":" + UrlConstant.DEFAULT_PORT + UrlConstant.WEB_DEFAULT_PATH + UrlConstant.WEB_URL_RES);
        webFragment_frequency.setArguments(bundle2);

        WebFragment webFragment_homework = new WebFragment();//作业量分析
        Bundle bundle3 = new Bundle();
        bundle3.putString("url", UrlConstant.DEFAULT_HTTP + UrlConstant.DEFAULT_DOMAIN + ":" + UrlConstant.DEFAULT_PORT + UrlConstant.WEB_DEFAULT_PATH + UrlConstant.WEB_URL_WORK);
        webFragment_homework.setArguments(bundle3);

        purchaseFragment = new PurchaseFragment();//RFID

        fragmentList = new ArrayList<>();
        Collections.addAll(fragmentList, wareFragment, webFragment_handle, webFragment_frequency, webFragment_homework , purchaseFragment);
        MyViewPageAdapter viewPageAdapter = new MyViewPageAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setOffscreenPageLimit(5);
        viewPager.setAdapter(viewPageAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabAdapter(new MyTabAdapter(this));
    }

    private void initClick() {
        tabLayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
//                fragmentList.get(position).initData();
            }

            @Override
            public void onTabReselected(TabView tab, int position) {
                showFragment = fragmentList.get(position);
                showFragment.initData();
            }
        });

        tvReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initReceiver() {
        intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_TIME_TICK);//每分钟变化
        timeChangeReceiver = new TimeChangeReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        time = 0;
        registerReceiver(timeChangeReceiver, intentFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(timeChangeReceiver);
    }

    class TimeChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (null == intent.getAction()) return;
            switch (intent.getAction()) {
                case Intent.ACTION_TIME_TICK:
                    if (null == time || null == period || 0 == period) return;
                    time++;
                    if (time % period == period - 1) {
                        showFragment.initData();
                    }
                    break;
            }
        }
    }

    //===========================================public=============================================

    /**
     * 仓库概览Tab页跳转
     */
    public void startFragment(BaseFragment fragment) {
        wareFragment.startFragment(fragment);
    }

}
